/* @(#)GSManufactureScanQueryAction.java    
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li     2006-2-13    新增
 *   Jenny Li     2006-3-4   将读取产品编码改成读取集中编码
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;

import com.hitrust.gs.bean.GSManufactureCategoryBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.form.GSManufactureScanQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSManufactureVO;
import com.hitrust.gs.vo.GSOrderVO;

/**
 * 产品浏览查询操作
 */
public class GSManufactureScanQueryAction extends GSBaseAction {

    public String execute(GSBaseForm aForm) throws Exception {
        
        GSManufactureScanQueryForm tForm = (GSManufactureScanQueryForm)aForm;
        GSManufactureProcessor tGSManufProcessor = new GSManufactureProcessor();
        //取得页面参数放入VO对象
        GSManufactureVO tGSManufactureVO = new GSManufactureVO();
        //tGSManufactureVO.gs_manufacture_id = tForm.getManufactureId();
        
        tGSManufactureVO.gs_concentricity_id = tForm.getConcentricityId();
        tGSManufactureVO.gs_manufacture_name = tForm.getManufactureName();
        tGSManufactureVO.gs_manufacturer_name = tForm.getManufacturerName();
        tGSManufactureVO.gs_manufacture_category1 = tForm.getManuftype1();
        tGSManufactureVO.gs_manufacture_category2 = tForm.getManuftype2();
        tGSManufactureVO.gs_manufacture_category3 = tForm.getManuftype3();
        //tGSManufactureVO.gs_manufacture_price_min = tForm.getManufunitpricemin();
        //tGSManufactureVO.gs_manufacture_price_max = tForm.getManufunitpricemax();
        tGSManufactureVO.gs_manufacture_brand = tForm.getManufactureBrand();
        tGSManufactureVO.gs_manufacture_spec = tForm.getManufactureSpec();
        tGSManufactureVO.gs_manufacture_type = tForm.getManufactureType();
        tGSManufactureVO.gs_provider_short_name = tForm.getProviderShortName();
        tGSManufactureVO.gs_drawing_peice_code = tForm.getDrawingPieceCode();
        tGSManufactureVO.gs_balance_mode=tForm.getBalancemode();
        String keyword = tForm.getKeyword();
        String iskeywordquery = (String)aForm.getRequest().getParameter("iskeywordquery");
        aForm.getRequest().setAttribute("iskeywordquery",iskeywordquery);
        
        
        
        try{
            //根据页面传入参数取得产品信息
            ArrayList tList = null;
            String[] tStr = this.getPageParams(aForm,tGSManufactureVO,keyword,iskeywordquery);
            //直接在此处于session中缓存querycond/querycondkey
            if("F".equals(iskeywordquery)){    
                String sql = tGSManufProcessor.getSql(tGSManufactureVO);//为产品导出构造查询方案sql
                aForm.getSession().setAttribute("querycond",sql);
                tList = tGSManufProcessor.getManufactureByOpt(tGSManufactureVO,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
            }
            else{             	
            	aForm.getSession().setAttribute("querycondkey",keyword);
            	GSManufactureVO vo = new GSManufactureVO();
                vo.gs_manufacture_brand = keyword;
                vo.gs_manufacture_name = keyword;
                vo.gs_manufacture_spec = keyword;
                vo.gs_manufacture_type = keyword;
                vo.gs_manufacturer_name = keyword;
                vo.gs_appender_property = keyword;
            	String sqlbuf = tGSManufProcessor.getSqlForKeywordInManufactureBrowse(vo,keyword);
            	aForm.getSession().setAttribute("querycond",sqlbuf);
                tList = tGSManufProcessor.getManufactureByKeywordForBrowse(sqlbuf,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
            }
            ArrayList tTempList=null;
            
            
            //放入ResultProcess对象为页面分页做准备
            ResultProcess tProc = new ResultProcess();
            tProc.setResult(tList);
            tForm.getRequest().setAttribute("manufacture",tProc);
            //存储分页参数
            tForm.getRequest().setAttribute("pageSize",tStr[1]);
            tForm.getRequest().setAttribute("totalRecords",tStr[2]);
            
            tGSManufProcessor.getManufactureOfLevel(tForm);

        }catch(GSException e){
            tForm.getRequest().setAttribute("action","A200101.do");
            throw e;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            tForm.getRequest().setAttribute("action","A200101.do");
            throw new GSException(GSException.E10030,GSException.E10030_MSG);
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }

    /**
     * 取得分页参数
     * @param aForm         基类form
     * @param aOrderVO      订单VO
     * @param aOrg          组织ArrayList对象
     * @return
     * @throws Exception
     */
    private String[] getPageParams(GSBaseForm aForm,GSManufactureVO tGSManufactureVO,String keyword,String iskeywordquery) throws Exception{
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        GSManufactureProcessor tProc = new GSManufactureProcessor();
        //根据页面传入的查询参数查询订单信息
        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
            if("F".equals(iskeywordquery)){                
                totalRecords = tProc.getManufactureCountByOpt(tGSManufactureVO);
            }
            else{ 
                totalRecords = tProc.getManufactureCountByKeywordForBrowse(keyword);
            }
            
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }
}
